<template>
  <div class="wrap">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="头像">
      </div>
      <!-- 表单区域 -->
      <el-form label-width="0px" ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="loginname">
          <el-input placeholder="请输入用户名" v-model="loginForm.loginname">
            <i slot="prefix" class="iconfont icon-yonghuming"></i>
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="loginForm.password" show-password prefix-icon="iconfont icon-mima">
          </el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item prop="eMail">
          <el-input placeholder="请输入邮箱" v-model="loginForm.eMail" prefix-icon="iconfont icon-youxiang">
          </el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btn">
          <el-button type="primary" round @click="btn_login">登录</el-button>
          <el-button type="info" round @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'Login',
  data() {
    // 自定义验证规则
    const eMailReg = /^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$/;
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入邮箱！'));
      } else {
        if (!eMailReg.test(this.loginForm.eMail)) {
          callback(new Error('邮箱格式错误！'))
        }
        callback();
      }
    };
    return {
      loginForm: {
        loginname: 'admin',  // 登录名
        password: '123456',  // 密码
        eMail: 'admin@local.com',  // 邮箱
      },
      // 登录验证规则
      loginFormRules: {
        loginname: [
          { required: true, message: '请输入用户名！', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符！', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码！', trigger: 'blur' },
          { min: 3, message: '最少3个字符', trigger: 'blur' },
        ],
        eMail: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
      },
    }
  },
  methods: {
    // 重置按钮
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    // 登录按钮
    btn_login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return

        // 发送请求
        const { data: res } = await this.$ajax.post('api/login', this.loginForm)
        if (res.code == 1) return this.$message.error(`登陆失败！${res.msg}`);
        this.$message.success('欢迎回来！')
        // 保存 token 到本地
        window.localStorage.setItem('token', res.token)
        // 路由跳转
        this.$router.push('/home')
      })
    }
  }
}
</script>
 
<style lang='less' scoped>
.wrap {
  height: 100%;
  background-color: rgb(174, 255, 221);

  .login_box {
    width: 450px;
    height: 350px;
    border: 1px solid orange;
    position: relative;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background-color: rgb(255, 225, 170);

    .avatar_box {
      width: 130px;
      height: 130px;
      border: 1px solid orange;
      border-radius: 50%;
      background-color: rgb(221, 239, 255);
      padding: 6px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);

      img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
      }
    }

    .login_form {
      width: 100%;
      position: absolute;
      box-sizing: border-box;
      padding: 0 10px;
      bottom: 0;

      .btn {
        display: flex;
        justify-content: end;
      }
    }
  }


}
</style>